package org.polaris.authority.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.polaris.authority.annotation.PolarisPermission;
import org.polaris.authority.domain.Menu;
import org.polaris.authority.dto.SetMenusDTO;
import org.polaris.authority.query.MenuQuery;
import org.polaris.authority.service.IMenuService;
import org.polaris.basic.tool.AjaxResult;
import org.polaris.basic.tool.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(value = "菜单管理")
@RequestMapping("/menu")
@PolarisPermission(name="菜单管理",descs = "菜单管理")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    @ApiOperation(value = "查询全部")
    @GetMapping
    @PolarisPermission(name="查询全部",descs = "查询全部")
    public AjaxResult selectAll(){
        try {
            List<Menu> menus = menuService.selectAll();
            return AjaxResult.result().setObj(menus);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
        }
    }
    @ApiOperation(value = "通过ID查询")
    @PolarisPermission(name="通过ID查询",descs = "通过ID查询")
    @GetMapping("getById/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id){
        try {
            Menu menu = menuService.selectById(id);
            return AjaxResult.result().setObj(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
        }
    }
    @ApiOperation(value = "通过ID删除")
    @PolarisPermission(name="通过ID删除",descs = "通过ID删除")
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id") Long id){
        try {
            menuService.deleteById(id);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
        }
    }

    @ApiOperation(value = "新增&修改,存在ID则修改反之则新增")
    @PutMapping("/saveMenu")
    @PolarisPermission(name="保存",descs = "新增&修改,存在ID则修改反之则新增")
    public AjaxResult save(@RequestBody Menu menu){
        if (StringUtils.isEmpty(menu.getId())){
            try {
                menuService.add(menu);
                return AjaxResult.result();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
            }

        }else {
            try {
                menuService.update(menu);
                return AjaxResult.result();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
            }
        }
    }
    @ApiOperation(value = "分页")
    @PostMapping
    @PolarisPermission(name="分页",descs = "分页")
    public AjaxResult paging(@RequestBody MenuQuery menuQuery){
        try {
            System.out.println(menuQuery.toString());
            PageResult paging = menuService.paging(menuQuery);
            System.out.println(paging);
            return AjaxResult.result().setObj(paging);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage(e.getMessage());
        }

    }


    @PutMapping("/saveMenus")
    @PolarisPermission(name = "设置菜单")
    public AjaxResult saveMenus(@RequestBody SetMenusDTO dto){
        try {
            System.out.println(dto);
            menuService.deleteById(dto.getEid());
            menuService.saveMenus(dto);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage("设置菜单失败!"+e.getMessage());
        }
    }

    @GetMapping("/getMenus/{eId}")
    @PolarisPermission(name = "获取菜单")
    public AjaxResult getMenus(@PathVariable("eId")Long eId){
        try {
            List<Long> menus = menuService.getMenus(eId);
            return AjaxResult.result().setObj(menus);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setSuccess(false).setMessage("获取菜单失败!"+e.getMessage());
        }
    }

    @PostMapping("/getMenuTree/{eId}")
    @PolarisPermission(name = "获取菜单树")
    public AjaxResult getMenuTree(@PathVariable("eId")Long eId){
        List<Menu> menus = menuService.getMenuTree(eId);
        return AjaxResult.result().setObj(menus);

    }
}
